package com.flc.gateway.codc;

import com.flc.gateway.model.Command;
import com.flc.gateway.utils.ByteUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author haizi
 * @date 2018/1/5
 */
public class CommandEncoder extends MessageToByteEncoder<Command> {

    private static int BAOTOU = 32523523;

    @Override
    protected void encode(ChannelHandlerContext ctx, Command cmd, ByteBuf buffer) throws Exception {

        System.out.println("下发指令:" + "module:" + cmd.getModule() +" cmd:" + ByteUtils.getHexString(cmd.getData()));
        //包头
        buffer.writeInt(BAOTOU);
        //module和cmd
        buffer.writeShort(cmd.getModule());
        //长度
        int length = cmd.getDataLength();
        buffer.writeInt(length);
        if (length > 0) {
            buffer.writeBytes(cmd.getData());
        }
    }
}
